<?php

class ProductsController extends AppController {

    public $name = 'Products';

    public function index($purchasing_committee_id) {
        $this->layout = "ajax";
        $this->set("purchasing_committee_id", $purchasing_committee_id);
        //envío los productos
        $this->Product->PurchasingCommittee->recursive = -1;
        $operating_plan_id = $this->Product->PurchasingCommittee->field('operating_plan_id', array('PurchasingCommittee.id' => $purchasing_committee_id));
        $this->set('operating_plan_id', $operating_plan_id);

        $this->recursive = -1;
        $products = $this->Product->find('all', array('conditions' => array('Product.purchasing_committee_id' => $purchasing_committee_id), 'fields' => array('Product.*'), 'order' => array('Product.id' => 'ASC')));
        $this->set('products', $products);
    }

    public function index_ejecucion($purchasing_committee_id) {
        $this->layout = "ajax";
        $this->set("purchasing_committee_id", $purchasing_committee_id);
        //envío los productos
        $this->Product->PurchasingCommittee->recursive = -1;
        $operating_plan_id = $this->Product->PurchasingCommittee->field('operating_plan_id', array('PurchasingCommittee.id' => $purchasing_committee_id));
        $this->set('operating_plan_id', $operating_plan_id);

        $this->recursive = -1;
        $products = $this->Product->find('all', array('conditions' => array('Product.purchasing_committee_id' => $purchasing_committee_id), 'fields' => array('Product.*'), 'order' => array('Product.id' => 'ASC')));
        $this->set('products', $products);
    }

    public function add($purchasing_committee_id) {
        $this->layout = "ajax";
        if (empty($this->data)) {
            $this->set('purchasing_committee_id', $purchasing_committee_id);
        } else {
            if ($this->Product->saveAll($this->data)) {
                $this->Session->setFlash('Datos guardados correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Products', 'action' => 'index', $this->data['Product']['purchasing_committee_id']));
            } else {
                $this->Session->setFlash('Error guardando datos', 'flash_custom');
                $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
            }
        }
    }

    public function edit($id) {
        $this->layout = "ajax";
        if (empty($this->data)) {
            $this->Product->recursive = -1;
            $this->data = $this->Product->find('first', array('conditions' => array('Product.id' => $id), 'fields' => array('Product.*')));
        } else {
            if ($this->Product->saveAll($this->data)) {
                $this->Session->setFlash('Datos guardados exitosamente', 'flash_custom');
                $this->redirect(array('controller' => 'Products', 'action' => 'index', $this->data['Product']['purchasing_committee_id']));
            } else {
                $this->Session->setFlash('Error editando datos', 'flash_custom');
                $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
            }
        }
    }

    public function edit_ejecucion($id) {
        $this->layout = "ajax";
        if (empty($this->data)) {
            $this->Product->recursive = -1;
            $this->data = $this->Product->find('first', array('conditions' => array('Product.id' => $id), 'fields' => array('Product.*')));
        } else {
            if ($this->Product->saveAll($this->data)) {
                $this->Session->setFlash('Datos guardados exitosamente', 'flash_custom');
                $this->redirect(array('controller' => 'Products', 'action' => 'index_ejecucion', $this->data['Product']['purchasing_committee_id']));
            } else {
                $this->Session->setFlash('Error editando datos', 'flash_custom');
                $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
            }
        }
    }

    public function delete($id, $purchasing_committee_id) {
        if ($this->Product->delete($id)) {
            $this->Session->setFlash('Información del producto borrada con éxito', 'flash_custom');
            $this->redirect(array('controller' => 'Products', 'action' => 'index', $purchasing_committee_id));
        }
    }

}

?>
